package models

import (
	"github.com/EDDYCJY/go-gin-example/pkg/e"
)

type ImageFramwork struct {
	// 支付记录id
	ID uint64 `gorm:"primary_key" json:"id"`
	// 镜像ID
	ImageID string `gorm:"column:image_id" json:"image_id"`
	//镜像的封面Log地址
	ImageUrl string `gorm:"column:image_url" json:"image_url"`
	//镜像名称
	ImageName string `gorm:"column:image_name" json:"image_name"`
	//镜像的描述
	ImageDesc string `gorm:"column:image_desc" json:"image_desc"`
	//镜像大小
	ImageSize string `gorm:"column:image_size" json:"image_size"`
	//镜像类型
	ImageMirrorType uint32 `gorm:"column:image_mirror_type" json:"image_mirror_type"`
	//镜像关键词
	ImageKeyword string `gorm:"column:image_keyword" json:"image_keyword"`
	//镜像评分
	ImageEvaluate uint32 `gorm:"column:image_evaluate" json:"image_evaluate"`
	//镜像下载次数
	ImageDnum uint32 `gorm:"column:image_dnum" json:"image_dnum"`
	//镜像点赞数
	ImageLike uint32 `gorm:"column:image_like" json:"image_like"`
	//集成软件
	ImageIntegratedSoftware string `gorm:"column:image_integrated_software" json:"image_integrated_software"`
	//镜像下载地址
	ImageDownAddr string `gorm:"column:image_down_addr" json:"image_down_addr"`
	//镜像标题
	ImageTitle string `gorm:"column:image_title" json:"image_title"`
	// 注册时间
	CreateTime DbTime `gorm:"column:create_time" json:"create_time"`
	// 更新时间
	UpdateTime DbTime `gorm:"column:update_time" json:"update_time"`
}

func NewImageFramwork() *ImageFramwork {
	return &ImageFramwork{}
}

func (ImageFramwork) TableName() string {
	return "framework"
}
func (i *ImageFramwork) GetListPage(page uint32, limit uint32, mirrorType int, keyword string) ([]*ImageFramwork, e.FResult) {
	var offset uint32
	if page <= 1 {
		offset = 0
	} else {
		offset = (page - 1) * limit
	}

	where := make(map[string]interface{})
	if keyword != "" {
		where["image_keyword"] = keyword
	}
	if mirrorType != 0 {
		where["image_mirror_type"] = mirrorType
	}
	var fras []*ImageFramwork
	err := db.Model(&ImageFramwork{}).Debug().Offset(offset).Limit(limit).Where(where).
		Find(&fras).Error
	if err != nil {
		return nil, e.FResult_ERROR
	}
	return fras, e.FResult_OK
}

//根据镜像id 查询详情信息
func (i *ImageFramwork) GetDetailByImageId(id string) (*ImageFramwork, e.FResult) {
	var res ImageFramwork
	var eres e.FResult
	err := db.Model(&ImageFramwork{}).Where(map[string]interface{}{
		"image_id": id,
	}).First(&res).Error
	if err != nil {
		eres.ErrCode = -1
		eres.ErrMsg = "镜像id错误"
	}
	return &res, eres
}

func (i *ImageFramwork) GetToal(mirrorType int, keyword string) int64 {
	where := make(map[string]interface{})
	if keyword != "" {
		where["image_keyword"] = keyword
	}
	if mirrorType != 0 {
		where["image_mirror_type"] = mirrorType
	}
	var count int64
	db.Model(&ImageFramwork{}).Where(where).Count(&count)
	return count
}
